{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a97f615-d5a9-4167-a19a-99d1cfda80ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "01:45AM 12s INFO [__init__]: TransmonBuilder with system_type TRANSMON registered to QuantumSystemRegistry\n",
      "01:45AM 12s INFO [__init__]: FluxoniumBuilder with system_type FLUXONIUM registered to QuantumSystemRegistry\n",
      "01:45AM 12s INFO [__init__]: TLResonatorBuilder with system_type TL_RESONATOR registered to QuantumSystemRegistry\n",
      "01:45AM 12s INFO [__init__]: LumpedResonatorBuilder with system_type LUMPED_RESONATOR registered to QuantumSystemRegistry\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "\n",
    "import scqubits as scq\n",
    "\n",
    "from qiskit_metal.analyses.quantization.lumped_capacitive import load_q3d_capacitance_matrix\n",
    "from qiskit_metal.analyses.quantization.lom_core_analysis import CompositeSystem, Cell, Subsystem, QuantumSystemRegistry\n",
    "                        \n",
    "from scipy.constants import speed_of_light as c_light\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ae62a64a-ad98-4656-8882-c617a783f244",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'TRANSMON': qiskit_metal.analyses.quantization.lom_core_analysis.TransmonBuilder,\n",
       " 'FLUXONIUM': qiskit_metal.analyses.quantization.lom_core_analysis.FluxoniumBuilder,\n",
       " 'TL_RESONATOR': qiskit_metal.analyses.quantization.lom_core_analysis.TLResonatorBuilder,\n",
       " 'LUMPED_RESONATOR': qiskit_metal.analyses.quantization.lom_core_analysis.LumpedResonatorBuilder}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "QuantumSystemRegistry.registry()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32717ec8-f907-4902-bc2c-f5a17a7516bc",
   "metadata": {},
   "source": [
    "# Example: two transmons coupled by a direct coupler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "772a4f4a-0881-499f-a370-3e69494a817a",
   "metadata": {},
   "source": [
    "### 1. load transmon cell Q3d simulation results\n",
    "Loading the Maxwell capacitance matrices for the design as shown in the screenshot below:\n",
    "\n",
    "<img src=\"two_transmons_direct_coupler.png\" width=600 height=600 />\n",
    "\n",
    "where we have two transmons (alice and bob) coupled to each other through a direct coupler. Each transmon is also coupled to its own readout reasonator.\n",
    "\n",
    "For a simple introduction on Maxwell capacitance matrix, check out the following resources:\n",
    "https://www.fastfieldsolvers.com/Papers/The_Maxwell_Capacitance_Matrix_WP110301_R02.pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4b1be2d-19f7-410d-a0ca-ed273907a630",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Imported capacitance matrix with UNITS: [fF] now converted to USER UNITS:[fF]                 from file:\n",
      "\t./Q1_TwoTransmon_CapMatrix.txt\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_f6843_row0_col0, #T_f6843_row1_col1, #T_f6843_row2_col2, #T_f6843_row3_col3, #T_f6843_row4_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 100.0%, transparent 100.0%);\n",
       "}\n",
       "#T_f6843_row0_col1, #T_f6843_row4_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 0.9%, transparent 0.9%);\n",
       "}\n",
       "#T_f6843_row0_col2 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 28.4%, transparent 28.4%);\n",
       "}\n",
       "#T_f6843_row0_col3 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.6%, transparent 15.6%);\n",
       "}\n",
       "#T_f6843_row0_col4, #T_f6843_row4_col0 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 38.4%, transparent 38.4%);\n",
       "}\n",
       "#T_f6843_row1_col0, #T_f6843_row1_col2, #T_f6843_row1_col3, #T_f6843_row1_col4, #T_f6843_row3_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "}\n",
       "#T_f6843_row2_col0 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 36.6%, transparent 36.6%);\n",
       "}\n",
       "#T_f6843_row2_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 0.0%, transparent 0.0%);\n",
       "}\n",
       "#T_f6843_row2_col3 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 7.0%, transparent 7.0%);\n",
       "}\n",
       "#T_f6843_row2_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 18.7%, transparent 18.7%);\n",
       "}\n",
       "#T_f6843_row3_col0 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 18.8%, transparent 18.8%);\n",
       "}\n",
       "#T_f6843_row3_col2 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 6.9%, transparent 6.9%);\n",
       "}\n",
       "#T_f6843_row3_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 36.5%, transparent 36.5%);\n",
       "}\n",
       "#T_f6843_row4_col2 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.5%, transparent 15.5%);\n",
       "}\n",
       "#T_f6843_row4_col3 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 28.5%, transparent 28.5%);\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_f6843_\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"blank level0\" >&nbsp;</th>\n",
       "      <th class=\"col_heading level0 col0\" >coupler_connector_pad_Q1</th>\n",
       "      <th class=\"col_heading level0 col1\" >ground_main_plane</th>\n",
       "      <th class=\"col_heading level0 col2\" >pad_bot_Q1</th>\n",
       "      <th class=\"col_heading level0 col3\" >pad_top_Q1</th>\n",
       "      <th class=\"col_heading level0 col4\" >readout_connector_pad_Q1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th id=\"T_f6843_level0_row0\" class=\"row_heading level0 row0\" >coupler_connector_pad_Q1</th>\n",
       "      <td id=\"T_f6843_row0_col0\" class=\"data row0 col0\" >59.20</td>\n",
       "      <td id=\"T_f6843_row0_col1\" class=\"data row0 col1\" >-37.28</td>\n",
       "      <td id=\"T_f6843_row0_col2\" class=\"data row0 col2\" >-2.01</td>\n",
       "      <td id=\"T_f6843_row0_col3\" class=\"data row0 col3\" >-19.11</td>\n",
       "      <td id=\"T_f6843_row0_col4\" class=\"data row0 col4\" >-0.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_f6843_level0_row1\" class=\"row_heading level0 row1\" >ground_main_plane</th>\n",
       "      <td id=\"T_f6843_row1_col0\" class=\"data row1 col0\" >-37.28</td>\n",
       "      <td id=\"T_f6843_row1_col1\" class=\"data row1 col1\" >246.33</td>\n",
       "      <td id=\"T_f6843_row1_col2\" class=\"data row1 col2\" >-39.79</td>\n",
       "      <td id=\"T_f6843_row1_col3\" class=\"data row1 col3\" >-39.86</td>\n",
       "      <td id=\"T_f6843_row1_col4\" class=\"data row1 col4\" >-37.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_f6843_level0_row2\" class=\"row_heading level0 row2\" >pad_bot_Q1</th>\n",
       "      <td id=\"T_f6843_row2_col0\" class=\"data row2 col0\" >-2.01</td>\n",
       "      <td id=\"T_f6843_row2_col1\" class=\"data row2 col1\" >-39.79</td>\n",
       "      <td id=\"T_f6843_row2_col2\" class=\"data row2 col2\" >93.05</td>\n",
       "      <td id=\"T_f6843_row2_col3\" class=\"data row2 col3\" >-30.61</td>\n",
       "      <td id=\"T_f6843_row2_col4\" class=\"data row2 col4\" >-19.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_f6843_level0_row3\" class=\"row_heading level0 row3\" >pad_top_Q1</th>\n",
       "      <td id=\"T_f6843_row3_col0\" class=\"data row3 col0\" >-19.11</td>\n",
       "      <td id=\"T_f6843_row3_col1\" class=\"data row3 col1\" >-39.86</td>\n",
       "      <td id=\"T_f6843_row3_col2\" class=\"data row3 col2\" >-30.61</td>\n",
       "      <td id=\"T_f6843_row3_col3\" class=\"data row3 col3\" >92.99</td>\n",
       "      <td id=\"T_f6843_row3_col4\" class=\"data row3 col4\" >-2.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_f6843_level0_row4\" class=\"row_heading level0 row4\" >readout_connector_pad_Q1</th>\n",
       "      <td id=\"T_f6843_row4_col0\" class=\"data row4 col0\" >-0.23</td>\n",
       "      <td id=\"T_f6843_row4_col1\" class=\"data row4 col1\" >-37.30</td>\n",
       "      <td id=\"T_f6843_row4_col2\" class=\"data row4 col2\" >-19.22</td>\n",
       "      <td id=\"T_f6843_row4_col3\" class=\"data row4 col3\" >-2.01</td>\n",
       "      <td id=\"T_f6843_row4_col4\" class=\"data row4 col4\" >59.33</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x18e331a00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# loading alice's simulation results\n",
    "path1 = './Q1_TwoTransmon_CapMatrix.txt'\n",
    "ta_mat, _, _, _ = load_q3d_capacitance_matrix(path1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4256eb88-ef19-4bbb-a5d3-b9cb8c0e968e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Imported capacitance matrix with UNITS: [fF] now converted to USER UNITS:[fF]                 from file:\n",
      "\t./Q2_TwoTransmon_CapMatrix.txt\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       "#T_c9ae2_row0_col0, #T_c9ae2_row1_col1, #T_c9ae2_row2_col2, #T_c9ae2_row3_col3, #T_c9ae2_row4_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 100.0%, transparent 100.0%);\n",
       "}\n",
       "#T_c9ae2_row0_col1, #T_c9ae2_row4_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 3.4%, transparent 3.4%);\n",
       "}\n",
       "#T_c9ae2_row0_col2, #T_c9ae2_row4_col3 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 27.6%, transparent 27.6%);\n",
       "}\n",
       "#T_c9ae2_row0_col3 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.5%, transparent 15.5%);\n",
       "}\n",
       "#T_c9ae2_row0_col4, #T_c9ae2_row4_col0 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 37.2%, transparent 37.2%);\n",
       "}\n",
       "#T_c9ae2_row1_col0, #T_c9ae2_row1_col2, #T_c9ae2_row1_col3, #T_c9ae2_row1_col4, #T_c9ae2_row3_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "}\n",
       "#T_c9ae2_row2_col0, #T_c9ae2_row3_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 35.3%, transparent 35.3%);\n",
       "}\n",
       "#T_c9ae2_row2_col1 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 0.0%, transparent 0.0%);\n",
       "}\n",
       "#T_c9ae2_row2_col3, #T_c9ae2_row3_col2 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 2.4%, transparent 2.4%);\n",
       "}\n",
       "#T_c9ae2_row2_col4 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.1%, transparent 15.1%);\n",
       "}\n",
       "#T_c9ae2_row3_col0 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.2%, transparent 15.2%);\n",
       "}\n",
       "#T_c9ae2_row4_col2 {\n",
       "  width: 10em;\n",
       "  height: 80%;\n",
       "  background: linear-gradient(90deg,#5fba7d 15.4%, transparent 15.4%);\n",
       "}\n",
       "</style>\n",
       "<table id=\"T_c9ae2_\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th class=\"blank level0\" >&nbsp;</th>\n",
       "      <th class=\"col_heading level0 col0\" >coupler_connector_pad_Q2</th>\n",
       "      <th class=\"col_heading level0 col1\" >ground_main_plane</th>\n",
       "      <th class=\"col_heading level0 col2\" >pad_bot_Q2</th>\n",
       "      <th class=\"col_heading level0 col3\" >pad_top_Q2</th>\n",
       "      <th class=\"col_heading level0 col4\" >readout_connector_pad_Q2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th id=\"T_c9ae2_level0_row0\" class=\"row_heading level0 row0\" >coupler_connector_pad_Q2</th>\n",
       "      <td id=\"T_c9ae2_row0_col0\" class=\"data row0 col0\" >64.52</td>\n",
       "      <td id=\"T_c9ae2_row0_col1\" class=\"data row0 col1\" >-38.63</td>\n",
       "      <td id=\"T_c9ae2_row0_col2\" class=\"data row0 col2\" >-2.18</td>\n",
       "      <td id=\"T_c9ae2_row0_col3\" class=\"data row0 col3\" >-22.93</td>\n",
       "      <td id=\"T_c9ae2_row0_col4\" class=\"data row0 col4\" >-0.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_c9ae2_level0_row1\" class=\"row_heading level0 row1\" >ground_main_plane</th>\n",
       "      <td id=\"T_c9ae2_row1_col0\" class=\"data row1 col0\" >-38.63</td>\n",
       "      <td id=\"T_c9ae2_row1_col1\" class=\"data row1 col1\" >267.40</td>\n",
       "      <td id=\"T_c9ae2_row1_col2\" class=\"data row1 col2\" >-49.28</td>\n",
       "      <td id=\"T_c9ae2_row1_col3\" class=\"data row1 col3\" >-49.30</td>\n",
       "      <td id=\"T_c9ae2_row1_col4\" class=\"data row1 col4\" >-38.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_c9ae2_level0_row2\" class=\"row_heading level0 row2\" >pad_bot_Q2</th>\n",
       "      <td id=\"T_c9ae2_row2_col0\" class=\"data row2 col0\" >-2.18</td>\n",
       "      <td id=\"T_c9ae2_row2_col1\" class=\"data row2 col1\" >-49.28</td>\n",
       "      <td id=\"T_c9ae2_row2_col2\" class=\"data row2 col2\" >121.38</td>\n",
       "      <td id=\"T_c9ae2_row2_col3\" class=\"data row2 col3\" >-45.24</td>\n",
       "      <td id=\"T_c9ae2_row2_col4\" class=\"data row2 col4\" >-23.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_c9ae2_level0_row3\" class=\"row_heading level0 row3\" >pad_top_Q2</th>\n",
       "      <td id=\"T_c9ae2_row3_col0\" class=\"data row3 col0\" >-22.93</td>\n",
       "      <td id=\"T_c9ae2_row3_col1\" class=\"data row3 col1\" >-49.30</td>\n",
       "      <td id=\"T_c9ae2_row3_col2\" class=\"data row3 col2\" >-45.24</td>\n",
       "      <td id=\"T_c9ae2_row3_col3\" class=\"data row3 col3\" >121.24</td>\n",
       "      <td id=\"T_c9ae2_row3_col4\" class=\"data row3 col4\" >-2.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th id=\"T_c9ae2_level0_row4\" class=\"row_heading level0 row4\" >readout_connector_pad_Q2</th>\n",
       "      <td id=\"T_c9ae2_row4_col0\" class=\"data row4 col0\" >-0.22</td>\n",
       "      <td id=\"T_c9ae2_row4_col1\" class=\"data row4 col1\" >-38.67</td>\n",
       "      <td id=\"T_c9ae2_row4_col2\" class=\"data row4 col2\" >-23.06</td>\n",
       "      <td id=\"T_c9ae2_row4_col3\" class=\"data row4 col3\" >-2.18</td>\n",
       "      <td id=\"T_c9ae2_row4_col4\" class=\"data row4 col4\" >64.70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x18e617ee0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# loading bob's simulation results\n",
    "path2 = './Q2_TwoTransmon_CapMatrix.txt'\n",
    "tb_mat, _, _, _ = load_q3d_capacitance_matrix(path2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ed9aaaf-2b6d-4371-a853-0a2f22e2b421",
   "metadata": {},
   "source": [
    "### 2. Create LOM cells from capacitance matrices\n",
    "#### Setting cell objects corresponding to the capacitance simulation results \n",
    "`coupler_connector_pad_Q1` and `coupler_connector_pad_Q2` refer to the same node corresponding to the direct coupler between the qubits but are different names in the capacitance matrix results file. In order to merge the two capacitance matrices in the LOM analysis, we need to rename them to be the same name. Also renaming `readout_connector_pad_Q1` and `readout_connector_pad_Q2` for clarity's sake. \n",
    "\n",
    "The following three parameters, `ind_dict`, `jj_dict`, `cj_dict`, all have the same structure. Each is a dictionary where the keys are tuples, giving the nodes that a junction is in between, and the values specifying the relevant values associated with the junction. `ind_dict` lets you specify the junction inductance in nH; `jj_dict` specifies the Josephson junction name (you can give the junction any name you wish; just need to be consistent with the name); `cj_dict` specifies the junction capacitance in fF."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1fd4755-9160-4895-b722-23ba6eaa8eb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cell 1: transmon Alice cell\n",
    "\n",
    "opt1 = dict(\n",
    "    node_rename = {'coupler_connector_pad_Q1': 'coupling', 'readout_connector_pad_Q1': 'readout_alice'}, \n",
    "    cap_mat = ta_mat,\n",
    "    ind_dict = {('pad_top_Q1', 'pad_bot_Q1'):10},  # junction inductance in nH\n",
    "    jj_dict = {('pad_top_Q1', 'pad_bot_Q1'):'j1'},\n",
    "    cj_dict = {('pad_top_Q1', 'pad_bot_Q1'):2}, # junction capacitance in fF\n",
    "\n",
    ")\n",
    "cell_1 = Cell(opt1)\n",
    "\n",
    "\n",
    "# cell 2: transmon Bob cell\n",
    "opt2 = dict(\n",
    "    node_rename = {'coupler_connector_pad_Q2': 'coupling', 'readout_connector_pad_Q2': 'readout_bob'},\n",
    "    cap_mat = tb_mat,\n",
    "    ind_dict = {('pad_top_Q2', 'pad_bot_Q2'): 12}, # junction inductance in nH\n",
    "    jj_dict = {('pad_top_Q2', 'pad_bot_Q2'):'j2'},\n",
    "    cj_dict = {('pad_top_Q2', 'pad_bot_Q2'):2}, # junction capacitance in fF\n",
    "\n",
    ")\n",
    "cell_2 = Cell(opt2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ceffcfc-6cc1-444e-9a92-2fe022b2385a",
   "metadata": {},
   "source": [
    "### 3. Create subsystems\n",
    "#### Creating the four subsystems, corresponding to the 2 qubits and the 2 readout resonators\n",
    "\n",
    "`Subsystem` takes three required arguments. The four currently supported system types are `TRANSMON`, `FLUXONIUM`, `TL_RESONATOR` (transmission line resonator) and `LUMPED_RESONATOR`. `nodes` lets you specify which node the subsystem should be mapped to in the cells. They should be consistent with the node names you have given previously. `q_opts` lets specify any optional parameters you want to give. For example, for qubits, you can provide `scqubits` parameters such as `ncut`, `truncated_dim` here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b2ec34e2-3a7e-4946-88c2-e497fc8d5836",
   "metadata": {},
   "outputs": [],
   "source": [
    "# subsystem 1: transmon Alice\n",
    "transmon_alice = Subsystem(name='transmon_alice', sys_type='TRANSMON', nodes=['j1'])\n",
    "\n",
    "\n",
    "# subsystem 2: transmon Bob\n",
    "transmon_bob = Subsystem(name='transmon_bob', sys_type='TRANSMON', nodes=['j2'])\n",
    "\n",
    "\n",
    "# subsystem 3: Alice readout resonator\n",
    "q_opts = dict(\n",
    "    f_res = 8, # resonator dressed frequency in GHz\n",
    "    Z0 = 50, # characteristic impedance in Ohm\n",
    "    vp = 0.404314 * c_light # phase velocity \n",
    ")\n",
    "res_alice = Subsystem(name='readout_alice', sys_type='TL_RESONATOR', nodes=['readout_alice'], q_opts=q_opts)\n",
    "\n",
    "\n",
    "# subsystem 4: Bob readout resonator\n",
    "q_opts = dict(\n",
    "    f_res = 7.6, # resonator dressed frequency in GHz\n",
    "    Z0 = 50, # characteristic impedance in Ohm\n",
    "    vp = 0.404314 * c_light # phase velocity \n",
    ")\n",
    "res_bob = Subsystem(name='readout_bob', sys_type='TL_RESONATOR', nodes=['readout_bob'], q_opts=q_opts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fee92273-fbdb-46cf-9b5b-950cb4516146",
   "metadata": {},
   "source": [
    "### 4. Create the composite system from the cells and the subsystems\n",
    "\n",
    "The LOM analysis will automatically remove all non-dynamic nodes. These are nodes that are either exclusively connected to only capacitors or only inductors and are not true degrees of freedom (please check out https://arxiv.org/pdf/2103.10344.pdf or https://cpb-us-w2.wpmucdn.com/campuspress.yale.edu/dist/2/3627/files/2020/10/Vool_Intro_quantum_electromagnetic_circuits.pdf for more information on this). Since we didn't (and didn't have to) simulate the readout resonators, the two nodes, `readout_alice` and `readout_bob`, connected only to other nodes capacitively as specified by the Maxwell capacitance matrices, would be eliminated. But they are actually dynamic nodes, connected to the inductors (not simulated) of the respective transmission lines and correspond to subsystems that we want to include in the Hamiltonian of the composite system, hence we list them as nodes to force keep with the parameter `nodes_force_keep`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0666953a-c1e4-49b2-8c8e-998edf97a070",
   "metadata": {},
   "outputs": [],
   "source": [
    "composite_sys = CompositeSystem(\n",
    "    subsystems=[transmon_alice, transmon_bob, res_alice, res_bob], \n",
    "    cells=[cell_1, cell_2], \n",
    "    grd_node='ground_main_plane',\n",
    "    nodes_force_keep=['readout_alice', 'readout_bob']\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a4a6c85-9d59-414a-ae20-e21e4a430db1",
   "metadata": {},
   "source": [
    "The `circuitGraph` object encapsulates the lumped model circuit analysis (i.e., LOM analysis) and contain the intermediate as well as final L and C matrices, their inverses needed to construct the Hamiltonian of the composite system. For more details on the meaning and calculation of these matrices, check out https://arxiv.org/pdf/2103.10344.pdf.\n",
    "\n",
    "Just to note that you can use the analysis without needing to know any detail about this object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7021dfb3-62e0-484f-8c67-b1d81f07a5ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "node_jj_basis:\n",
      "-------------\n",
      "\n",
      "['j1', 'pad_bot_Q1', 'j2', 'pad_bot_Q2', 'readout_alice', 'readout_bob', 'coupling']\n",
      "\n",
      "nodes_keep:\n",
      "-------------\n",
      "\n",
      "['j1', 'j2', 'readout_alice', 'readout_bob']\n",
      "\n",
      "\n",
      "L_inv_k (reduced inverse inductance matrix):\n",
      "-------------\n",
      "\n",
      "                j1        j2  readout_alice  readout_bob\n",
      "j1             0.1  0.000000            0.0          0.0\n",
      "j2             0.0  0.083333            0.0          0.0\n",
      "readout_alice  0.0  0.000000            0.0          0.0\n",
      "readout_bob    0.0  0.000000            0.0          0.0\n",
      "\n",
      "C_k (reduced capacitance matrix):\n",
      "-------------\n",
      "\n",
      "                      j1         j2  readout_alice  readout_bob\n",
      "j1             63.185549  -0.766012       8.318893    -0.323188\n",
      "j2             -0.766012  84.343548      -0.342145    10.039921\n",
      "readout_alice   8.318893  -0.342145      55.591197    -0.144354\n",
      "readout_bob    -0.323188  10.039921      -0.144354    60.347427\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cg = composite_sys.circuitGraph()\n",
    "print(cg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26229dbe-9842-425a-b2fd-946e17b5f29b",
   "metadata": {},
   "source": [
    "### 5. Generate the hilberspace from the composite system, leveraging the scqubits package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "38e01cc9-e011-48ac-9069-ee2d597d89cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HilbertSpace:  subsystems\n",
      "-------------------------\n",
      "\n",
      "Transmon------------| [Transmon_1]\n",
      "                    | EJ: 16346.15128067812\n",
      "                    | EC: 312.756868730393\n",
      "                    | ng: 0.001\n",
      "                    | ncut: 22\n",
      "                    | truncated_dim: 10\n",
      "                    |\n",
      "                    | dim: 45\n",
      "\n",
      "\n",
      "Transmon------------| [Transmon_2]\n",
      "                    | EJ: 13621.792733898432\n",
      "                    | EC: 234.32409269967633\n",
      "                    | ng: 0.001\n",
      "                    | ncut: 22\n",
      "                    | truncated_dim: 10\n",
      "                    |\n",
      "                    | dim: 45\n",
      "\n",
      "\n",
      "Oscillator----------| [Oscillator_1]\n",
      "                    | E_osc: 8000\n",
      "                    | l_osc: None\n",
      "                    | truncated_dim: 3\n",
      "                    |\n",
      "                    | dim: 3\n",
      "\n",
      "\n",
      "Oscillator----------| [Oscillator_2]\n",
      "                    | E_osc: 7600.0\n",
      "                    | l_osc: None\n",
      "                    | truncated_dim: 3\n",
      "                    |\n",
      "                    | dim: 3\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "hilbertspace = composite_sys.create_hilbertspace()\n",
    "print(hilbertspace)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91ec7014-3371-49d5-8549-503bd3961276",
   "metadata": {},
   "source": [
    "`add_interaction()` adds the interaction terms between the subsystems. Currently, capacitive coupling is supported (which is extracted by from off-diagonal elements in the C matrices, see *eqn 12, 13* in https://arxiv.org/pdf/2103.10344.pdf ) and contribute to the interaction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ef9ca844-eb2b-47ef-b7d8-9b9e4670738c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "Quantum object: dims = [[10, 10, 3, 3], [10, 10, 3, 3]], shape = (900, 900), type = oper, isherm = True\\begin{equation*}\\left(\\begin{array}{*{11}c}-2.438\\times10^{+04} & 0.108j & 0.0 & 0.127j & -0.436 & \\cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\-0.108j & -1.678\\times10^{+04} & 0.153j & 0.436 & 0.127j & \\cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & -0.153j & -9.184\\times10^{+03} & 0.0 & 0.617 & \\cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\-0.127j & 0.436 & 0.0 & -1.638\\times10^{+04} & 0.108j & \\cdots & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\-0.436 & -0.127j & 0.617 & -0.108j & -8.784\\times10^{+03} & \\cdots & -1.839\\times10^{-08} & 0.0 & 0.0 & 0.0 & 0.0\\\\\\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\ddots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots\\\\0.0 & 0.0 & 0.0 & 0.0 & -1.839\\times10^{-08} & \\cdots & 7.287\\times10^{+04} & 706.553j & 0.617 & 862.863j & -0.872\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \\cdots & -706.553j & 8.047\\times10^{+04} & 0.0 & 0.872 & 862.863j\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \\cdots & 0.617 & 0.0 & 7.327\\times10^{+04} & 499.608j & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \\cdots & -862.863j & 0.872 & -499.608j & 8.087\\times10^{+04} & 706.553j\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & \\cdots & -0.872 & -862.863j & 0.0 & -706.553j & 8.847\\times10^{+04}\\\\\\end{array}\\right)\\end{equation*}"
      ],
      "text/plain": [
       "Quantum object: dims = [[10, 10, 3, 3], [10, 10, 3, 3]], shape = (900, 900), type = oper, isherm = True\n",
       "Qobj data =\n",
       "[[-24384.00898603+0.00000000e+00j      0.        +1.08232559e-01j\n",
       "       0.        +0.00000000e+00j ...      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j      0.        +0.00000000e+00j]\n",
       " [     0.        -1.08232559e-01j -16784.00898603+0.00000000e+00j\n",
       "       0.        +1.53063953e-01j ...      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j      0.        +0.00000000e+00j]\n",
       " [     0.        +0.00000000e+00j      0.        -1.53063953e-01j\n",
       "   -9184.00898603+0.00000000e+00j ...      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j      0.        +0.00000000e+00j]\n",
       " ...\n",
       " [     0.        +0.00000000e+00j      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j ...  73269.11342615+0.00000000e+00j\n",
       "       0.        +4.99608155e+02j      0.        +0.00000000e+00j]\n",
       " [     0.        +0.00000000e+00j      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j ...      0.        -4.99608155e+02j\n",
       "   80869.11342615+0.00000000e+00j      0.        +7.06552629e+02j]\n",
       " [     0.        +0.00000000e+00j      0.        +0.00000000e+00j\n",
       "       0.        +0.00000000e+00j ...      0.        +0.00000000e+00j\n",
       "       0.        -7.06552629e+02j  88469.11342615+0.00000000e+00j]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hilbertspace = composite_sys.add_interaction()\n",
    "hilbertspace.hamiltonian()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5e863ce-26e3-41da-a503-7691f55585c3",
   "metadata": {},
   "source": [
    "### 6. Print the results\n",
    "\n",
    "Print the calculated Hamiltonian parameters from diagonalized composite system Hamiltonian.\n",
    "\n",
    "The diagonal elements of the $\\chi$ matrix are the anharmonicities of the respective subsystems and the off-diagonal the dispersive shifts between them. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "77fa60e2-da3e-45f7-922d-c470070131c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finished eigensystem.     \n",
      "\n",
      "system frequencies in GHz:\n",
      "--------------------------\n",
      "{'transmon_alice': 6.053360688806868, 'transmon_bob': 4.7989883222888094, 'readout_alice': 8.009054820710865, 'readout_bob': 7.604412010766995}\n",
      "\n",
      "Chi matrices in MHz\n",
      "--------------------------\n",
      "                transmon_alice  transmon_bob  readout_alice  readout_bob\n",
      "transmon_alice     -353.239816     -0.542895      -4.132854    -0.003120\n",
      "transmon_bob         -0.542895   -263.940098      -0.001154    -1.460416\n",
      "readout_alice        -4.132854     -0.001154       4.283111    -0.000017\n",
      "readout_bob          -0.003120     -1.460416      -0.000017     3.829744\n"
     ]
    }
   ],
   "source": [
    "hamiltonian_results = composite_sys.hamiltonian_results(hilbertspace, evals_count=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5138689d-3384-4763-aeef-ad9a7d481ae8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>transmon_alice</th>\n",
       "      <th>transmon_bob</th>\n",
       "      <th>readout_alice</th>\n",
       "      <th>readout_bob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>transmon_alice</th>\n",
       "      <td>-353.239816</td>\n",
       "      <td>-0.542895</td>\n",
       "      <td>-4.132854</td>\n",
       "      <td>-0.003120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>transmon_bob</th>\n",
       "      <td>-0.542895</td>\n",
       "      <td>-263.940098</td>\n",
       "      <td>-0.001154</td>\n",
       "      <td>-1.460416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>readout_alice</th>\n",
       "      <td>-4.132854</td>\n",
       "      <td>-0.001154</td>\n",
       "      <td>4.283111</td>\n",
       "      <td>-0.000017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>readout_bob</th>\n",
       "      <td>-0.003120</td>\n",
       "      <td>-1.460416</td>\n",
       "      <td>-0.000017</td>\n",
       "      <td>3.829744</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                transmon_alice  transmon_bob  readout_alice  readout_bob\n",
       "transmon_alice     -353.239816     -0.542895      -4.132854    -0.003120\n",
       "transmon_bob         -0.542895   -263.940098      -0.001154    -1.460416\n",
       "readout_alice        -4.132854     -0.001154       4.283111    -0.000017\n",
       "readout_bob          -0.003120     -1.460416      -0.000017     3.829744"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hamiltonian_results['chi_in_MHz'].to_dataframe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb0e4af6-4a26-4662-8315-e30753d4f649",
   "metadata": {},
   "source": [
    "The $\\chi$'s between the subsystems are based on the coupling strengths, $\\it{g}$'s between them (which are computed using the coupling capacitance (currently capacitive coupling is supported) and zero point fluctuations of the subsystem's charge operator at the coupling location)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c9411ae7-3b22-4d57-b37a-03c43939a6c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                transmon_alice  transmon_bob  readout_alice  readout_bob\n",
       "transmon_alice        0.000000     20.115410    -129.897537     3.275638\n",
       "transmon_bob         20.115410      0.000000       2.678608  -111.508230\n",
       "readout_alice      -129.897537      2.678608       0.000000     0.436190\n",
       "readout_bob           3.275638   -111.508230       0.436190     0.000000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "composite_sys.compute_gs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ef3355e3-8fcd-4be5-976c-175068260b1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>transmon_alice</th>\n",
       "      <th>transmon_bob</th>\n",
       "      <th>readout_alice</th>\n",
       "      <th>readout_bob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>transmon_alice</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>20.115410</td>\n",
       "      <td>-129.897537</td>\n",
       "      <td>3.275638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>transmon_bob</th>\n",
       "      <td>20.115410</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.678608</td>\n",
       "      <td>-111.508230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>readout_alice</th>\n",
       "      <td>-129.897537</td>\n",
       "      <td>2.678608</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.436190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>readout_bob</th>\n",
       "      <td>3.275638</td>\n",
       "      <td>-111.508230</td>\n",
       "      <td>0.436190</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                transmon_alice  transmon_bob  readout_alice  readout_bob\n",
       "transmon_alice        0.000000     20.115410    -129.897537     3.275638\n",
       "transmon_bob         20.115410      0.000000       2.678608  -111.508230\n",
       "readout_alice      -129.897537      2.678608       0.000000     0.436190\n",
       "readout_bob           3.275638   -111.508230       0.436190     0.000000"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "composite_sys.compute_gs().to_dataframe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fb26d9ba-73bb-41ab-a9fc-02d6def23f31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'EJ': 16346.15128067812,\n",
       " 'EC': 312.756868730393,\n",
       " 'Q_zpf': 3.204353268e-19,\n",
       " 'default_charge_op': Operator(op=array([[-22,   0,   0, ...,   0,   0,   0],\n",
       "        [  0, -21,   0, ...,   0,   0,   0],\n",
       "        [  0,   0, -20, ...,   0,   0,   0],\n",
       "        ...,\n",
       "        [  0,   0,   0, ...,  20,   0,   0],\n",
       "        [  0,   0,   0, ...,   0,  21,   0],\n",
       "        [  0,   0,   0, ...,   0,   0,  22]]), add_hc=False)}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transmon_alice.h_params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e23dff63-4823-46dd-8eb3-51f45a87b0b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'EJ': 13621.792733898432,\n",
       " 'EC': 234.32409269967633,\n",
       " 'Q_zpf': 3.204353268e-19,\n",
       " 'default_charge_op': Operator(op=array([[-22,   0,   0, ...,   0,   0,   0],\n",
       "        [  0, -21,   0, ...,   0,   0,   0],\n",
       "        [  0,   0, -20, ...,   0,   0,   0],\n",
       "        ...,\n",
       "        [  0,   0,   0, ...,  20,   0,   0],\n",
       "        [  0,   0,   0, ...,   0,  21,   0],\n",
       "        [  0,   0,   0, ...,   0,   0,  22]]), add_hc=False)}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transmon_bob.h_params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f119975-2123-4b8e-8c9d-b2f6e8995b37",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "487fc71460a2c3509d052c1ab59d4d4ee7aa0fe9ce9e1b164929d34b719b0588"
  },
  "kernelspec": {
   "display_name": "metal",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
